// pages/news/my-friend/my-friend.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hiddenPage: false,
    hiddenCon: false, //搜索时隐藏
    searchText: '', //搜索内容
    character: ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V","W", "X", "Y", "Z"],
    typeList:[],
    newRecord: [], //历史记录

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    wx.showLoading({
      title: '加载中',
    })
    this.getMyFriend();
  },

  getMyFriend:function(){
    var that = this;
    wx.request({
      url: app.globalData.host + 'mine/myFriend',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'GET',
      data: {
        user_id:wx.getStorageSync('userId')
      },
      success(res) {
        wx.hideLoading();
        console.log(res)
        that.setData({
          typeList:res.data.data.list
        })
      },
      fail(res) {},
      complete(res) {},
    })
  },

  // 输入搜索内容
  searchInput: function (e) {
    let that = this;
    that.setData({
      searchText: e.detail.value
    })


    let searchText = that.data.searchText;
    console.log('searchText=' + searchText)
    if (searchText.length == 0) {
      that.setData({
        // newRecord: wx.getStorageSync('recentSearch') || [],
        hiddenCon: false,
      })
      return;
    }
    // //判断searchText是否在数组中
    // if (that.data.newRecord.indexOf(searchText) != -1) {
    //   console.log('最近搜索过')
    // } else {
    //   //否，push
    //   that.data.newRecord.push(searchText)
    // };
    // //把新的newRecord存储到缓存中
    // wx.setStorageSync('recentSearch', that.data.newRecord);
    // wx.getStorage({
    //   key: 'recentSearch',
    //   success: function (res) {
    //     console.log(res)
    //   }
    // })

    let typeList = that.data.typeList;
    let typeListArr = [];
    for (let key in typeList) {
      typeListArr.push(typeList[key])
    }

    let len = typeListArr.length;
    let arr = []; //搜索结果数组
    for (var i = 0; i < len; i++) {
      //如果字符串中不包含目标字符会返回-1
      let len2 = typeListArr[i].length;
      for (let j = 0; j < len2; j++) {
        var crr = JSON.stringify(typeListArr[i][j])
        if (crr.indexOf(searchText) >= 0) {
          arr.push(typeListArr[i][j]);
        }
      }
    }
    console.log('搜索结果=====')
    console.log(arr)
    that.setData({
      hiddenCon: true,
      searchRes: arr,
    })
    if (arr.length == 0) {
      wx.showToast({
        title: '很抱歉，没有搜索到您查找的内容!',
        icon: 'none',
        duration: 3000,
        success: function (res) {
          // that.setData({
          //   hiddenCon: true
          // })
        },
        fail: function (res) { },
        complete: function (res) { },
      })
    }

  },


  toView: function (e) { //滚动到指定位置
    console.log(e)
    let id = e.currentTarget.dataset.id;
    console.log(id)
    wx.pageScrollTo({
      selector: '#' + id,
      duration: 300
    })
  },


  openChat: function (e) { //打开聊天页面
    var that = this;
    var id = e.currentTarget.dataset.id;
    console.log(id)
    if (id != wx.getStorageSync('userId')) {
      wx.navigateTo({
        url: '/pages/chat/chat?id=' + id,
      })
    }
  },









  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})